package com.ruicar.afs.cloud.afscase.channel.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.channel.common.Constants;
import com.ruicar.afs.cloud.afscase.channel.condition.CarDealerCondition;
import com.ruicar.afs.cloud.afscase.channel.condition.ChannelBaseInfoCondition;
import com.ruicar.afs.cloud.afscase.channel.dto.ChannelCaseInfoDTO;
import com.ruicar.afs.cloud.afscase.channel.entity.*;
import com.ruicar.afs.cloud.afscase.channel.enums.FeeTypeEnum;
import com.ruicar.afs.cloud.afscase.channel.service.*;
import com.ruicar.afs.cloud.afscase.channel.utils.Common;
import com.ruicar.afs.cloud.afscase.channel.vo.AllCarDealerVo;
import com.ruicar.afs.cloud.afscase.channel.vo.ChannelCaseInfoVo;
import com.ruicar.afs.cloud.afscase.channel.vo.ChannelLimitTakeUpOrLeaseVo;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.enums.BusinessType;
import com.ruicar.afs.cloud.parameter.commom.condition.AddressParamCondition;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysAddressParam;
import com.ruicar.afs.cloud.parameter.commom.service.TsysAddressParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.ruicar.afs.cloud.afscase.channel.common.Constants.BUSINESS_TYPE_NEW_CAR;
import static com.ruicar.afs.cloud.afscase.channel.common.Constants.BUSINESS_TYPE_OLD_CAR;

/**
 * @ClassName:ChannelCaseInfoController
 * @Description: 同步渠道信息案件服务-controller
 * @Author:mingzhi.li
 * @Date:2020/6/9 14:06
 * @Version: V1.0
 **/

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/channelCaseInfo")
@Api(value = "channelCaseInfo", description = "同步进件上线申请信息")
public class ChannelCaseInfoController {
    private final TsysAddressParamService service;
    /**
     * 同步渠道信息保存
     */
    @Autowired
    private ChannelBaseInfoService channelBusiInfoService;

    /**
     * 同步风控信息
     */
    @Autowired
    private ChannelRiskInfoService channelRiskInfoService;

    /**
     * 同步收款账号信息
     */
    @Autowired
    private ChannelAccountInfoService channelAccountInfoService;

    /**
     * 同步保证金信息
     */
    @Autowired
    private ChannelQuotaInfoService channelQuotaInfoService;

    /**
     * 同步授权区域信息
     */
    @Autowired
    private ChannelAuthorizeRegionService channelAuthorizeRegionService;

    /**
     * 同步授权车型信息
     */
    @Autowired
    private ChannelAuthorizeVehicleService channelAuthorizeVehicleService;

    @Autowired
    private ChannelCoopeCardealerService channelCoopeCardealerService;

    private final ChannelVoucherInfoService channelVoucherInfoService;

    @PostMapping(value = "/saveOrUpdate")
    @ApiOperation(value = "保存渠道信息")
    @SysLog("保存渠道请信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse saveOrUpdate(@RequestBody ChannelCaseInfoVo vo) {
        if (vo.getChannelBaseInfo().getId() == null) {
            return IResponse.fail("保存失败");
        }
        //业务类型：新车二手车
        String businessType = vo.getBusinessType();
        //基本信息同步
        ChannelBaseInfo channelBaseInfo = vo.getChannelBaseInfo();
        if (channelBaseInfo != null) {
            ChannelBaseInfo baseInfo = channelBusiInfoService.getOne(Wrappers.<ChannelBaseInfo>query().lambda().eq(ChannelBaseInfo::getId, channelBaseInfo.getId()));
            //保存前先删除数据
            if (baseInfo != null) {
                channelBusiInfoService.updateById(channelBaseInfo);
            } else {
                channelBaseInfo.setChannelId(channelBaseInfo.getId());
                channelBaseInfo.setId(channelBaseInfo.getId());
                channelBusiInfoService.save(channelBaseInfo);
            }

            //渠道风控信息同步
            List<ChannelRiskInfo> channelRiskInfoList = vo.getRisk();
            List<ChannelRiskInfo> channelRiskInfoTempList = channelRiskInfoService.list(Wrappers.<ChannelRiskInfo>query().lambda()
                    .eq(ChannelRiskInfo::getChannelId, channelBaseInfo.getId())
                    .eq(ChannelRiskInfo::getBusinessType, businessType));
            //同步信息前删除数据
            if (channelRiskInfoTempList != null && channelRiskInfoTempList.size() > 0) {
                for (ChannelRiskInfo risk : channelRiskInfoTempList) {
                    channelRiskInfoService.removeById(risk.getId());
                }
            }
            if (channelRiskInfoList != null && channelRiskInfoList.size() > 0) {
                channelRiskInfoService.saveBatch(channelRiskInfoList);
            }

            //渠道同步收款账号表
            List<ChannelReceivablesAccount> channelReceivablesAccountList = vo.getAcount();
            List<ChannelReceivablesAccount> accountList = channelAccountInfoService.list(Wrappers.<ChannelReceivablesAccount>query().lambda()
                    .eq(ChannelReceivablesAccount::getChannelId, channelBaseInfo.getId().toString())
                    .eq(ChannelReceivablesAccount::getAccountAttribute, businessType));
            if (channelReceivablesAccountList != null && channelReceivablesAccountList.size() > 0) {
                if (accountList != null && accountList.size() > 0) {
                    //同步信息前删除数据
                    for (ChannelReceivablesAccount account : accountList) {
                        channelAccountInfoService.removeById(account.getId());
                    }
                }
                if (channelReceivablesAccountList != null && channelReceivablesAccountList.size() > 0) {
                    channelAccountInfoService.saveBatch(channelReceivablesAccountList);
                }
            }

            //渠道同步额度信息表
            List<ChannelQuotaInfo> channelQuotaInfoList = vo.getQuota();
            List<ChannelQuotaInfo> quotaInfoList = channelQuotaInfoService.list(Wrappers.<ChannelQuotaInfo>query().lambda()
                    .eq(ChannelQuotaInfo::getChannelId, channelBaseInfo.getId())
                    .eq(ChannelQuotaInfo::getBusinessType, businessType));
            if (quotaInfoList != null && quotaInfoList.size() > 0) {
                //同步信息前删除数据
                for (ChannelQuotaInfo quotaInfo : quotaInfoList) {
                    channelQuotaInfoService.removeById(quotaInfo.getId());
                }
            }
            if (channelQuotaInfoList != null && channelQuotaInfoList.size() > 0) {
                channelQuotaInfoService.saveBatch(channelQuotaInfoList);
            }

            //渠道同步授权区域信息表
            List<ChannelAuthorizeRegion> channelAuthorizeRegionList = vo.getRegion();
            channelAuthorizeRegionService.remove(Wrappers.<ChannelAuthorizeRegion>query().lambda()
                    .eq(ChannelAuthorizeRegion::getChannelId, channelBaseInfo.getId())
                    .eq(ChannelAuthorizeRegion::getBusinessType, businessType));
            if (channelAuthorizeRegionList != null && channelAuthorizeRegionList.size() > 0) {
                int size = channelAuthorizeRegionList.size();
                if (size <= 1000) {
                    channelAuthorizeRegionService.saveOrUpdateBatch(channelAuthorizeRegionList);
                } else {
                    for (int i = 0; i < size / 1000; i++) {
                        if (size / 1000 - i > 1) {
                            channelAuthorizeRegionService.saveOrUpdateBatch(channelAuthorizeRegionList.subList(1000 * i, 1000 * i + 999));
                        } else {
                            channelAuthorizeRegionService.saveOrUpdateBatch(channelAuthorizeRegionList.subList(1000 * i, size - 1));
                        }
                    }
                }
            }

            //渠道同步授权车型
            List<ChannelAuthorizeVehicle> authorizeVehicleList = vo.getVehicle();
            channelAuthorizeVehicleService.remove(Wrappers.<ChannelAuthorizeVehicle>query().lambda()
                    .eq(ChannelAuthorizeVehicle::getChannelId, channelBaseInfo.getId())
                    .eq(ChannelAuthorizeVehicle::getBusinessType, businessType));
            if (authorizeVehicleList != null && authorizeVehicleList.size() > 0) {
                int size = authorizeVehicleList.size();
                if (size <= 1000) {
                    channelAuthorizeVehicleService.saveBatch(authorizeVehicleList);
                } else {
                    for (int i = 0; i < size / 1000; i++) {
                        if (size / 1000 - i > 1) {
                            channelAuthorizeVehicleService.saveBatch(authorizeVehicleList.subList(1000 * i, 1000 * i + 999));
                        } else {
                            channelAuthorizeVehicleService.saveBatch(authorizeVehicleList.subList(1000 * i, size - 1));
                        }
                    }
                }
            }
            //测试凭证 异常暂时捕捉
            try {
                //判断是否保证金且保证金金额不为零
                if (vo.getRisk() != null && vo.getRisk().size() > 0 && vo.getRisk().get(0).getChannelDepositPaidIn() != null && vo.getRisk().get(0).getChannelDepositPaidIn().compareTo(BigDecimal.ZERO) != 0) {
                    //凭证信息保存并推送数据
                    List<ChannelVoucherInfo> list = new ArrayList<>();
                    //保证金实收
                    BigDecimal channelDepositPaidIn = vo.getRisk().get(0).getChannelDepositPaidIn();
                    ChannelVoucherInfo info = new ChannelVoucherInfo();
                    info.setChannelId(channelBaseInfo.getChannelId());
                    info.setFeeType(FeeTypeEnum.BOND);
                    info.setAmount(channelDepositPaidIn);
                    info.setEventId(channelBaseInfo.getEventId());
                    // 业务类型
                    if (Constants.BUSINESS_TYPE_NEW_CAR.equals(businessType)) { //新车
                        info.setBusinessType(BusinessType.NEW_CAR);
                    } else if (Constants.BUSINESS_TYPE_OLD_CAR.equals(businessType)) {//新车
                        info.setBusinessType(BusinessType.OLD_CAR);
                    } else {
                        Assert.isTrue(false, MessageFormat.format("业务类型:{0},暂不支持！", businessType));
                    }
                    list.add(info);
                    channelVoucherInfoService.saveVoucher(list, channelBaseInfo.getChannelFullName());
                }
            } catch (Exception e) {
                log.error("凭证流水保存失败");
            }
        }
        return IResponse.success("保存成功");
    }

    @PostMapping(value = "/updateGrade")
    @ApiOperation(value = "更新合作商评级")
    @SysLog("更新合作商评级")
    @Transactional(rollbackFor = Exception.class)
    public IResponse updateGrade(@RequestBody ChannelRiskInfo channelRiskInfo) {
        if (channelRiskInfo.getChannelId() == null) {
            return IResponse.fail("更新合作商评级失败，必传参数id为空");
        }
        try {
            ChannelRiskInfo channelRiskInfo1 = channelRiskInfoService.getOne(Wrappers.<ChannelRiskInfo>query().lambda()
                    .eq(ChannelRiskInfo::getChannelId, channelRiskInfo.getChannelId())
                    .eq(ChannelRiskInfo::getBusinessType, channelRiskInfo.getBusinessType()));
            if (channelRiskInfo1 != null) {
                channelRiskInfo1.setChannelGrade(channelRiskInfo.getChannelGrade());
                channelRiskInfoService.updateById(channelRiskInfo1);
            }
        } catch (Exception e) {
            log.error("更新合作商评级失败,事务回滚");
            //手动开启事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return IResponse.fail("更新合作商评级失败，请联系管理员！");
        }
        return IResponse.success("更新合作商评级成功！");
    }

    @PostMapping(value = "/limitTakeUpOrLease")
    @ApiOperation(value = "渠道额度占用与释放")
    @SysLog("渠道额度占用与释放")
    @Transactional(rollbackFor = Exception.class)
    public IResponse limitTakeUpOrLease(@RequestBody ChannelLimitTakeUpOrLeaseVo vo) {
        if (StrUtil.isBlank(vo.getChannelId())) {
            return IResponse.fail("渠道id不能为空！");
        }
        if (StrUtil.isBlank(vo.getQuotaType())) {
            return IResponse.fail("额度类型不能为空！");
        }
        if (StrUtil.isBlank(vo.getOperType())) {
            return IResponse.fail("额度操作类型(占用或释放)不能为空！");
        }
        if (StrUtil.isBlank(vo.getBusinessType())) {
            return IResponse.fail("业务类型不能为空！");
        }
        BigDecimal quotaAmount = vo.getQuotaAmount() == null ? BigDecimal.ZERO : vo.getQuotaAmount();
        if (quotaAmount.compareTo(BigDecimal.ZERO) == 0 || quotaAmount.compareTo(BigDecimal.ZERO) == -1) {
            return IResponse.fail("额度必须大于0!");
        }
        List<ChannelQuotaInfo> quotaInfoList = channelQuotaInfoService.list(Wrappers.<ChannelQuotaInfo>query().lambda()
                .eq(ChannelQuotaInfo::getChannelId, vo.getChannelId())
                .eq(ChannelQuotaInfo::getQuotaType, vo.getQuotaType())
                .eq(ChannelQuotaInfo::getBusinessType, vo.getBusinessType()));
        if (quotaInfoList == null && quotaInfoList.size() == 0) {
            return IResponse.fail("渠道不存在或额度类型不存在或业务类型不存在!");
        }
        channelQuotaInfoService.updateQuotaLimit(vo, quotaInfoList);

        return IResponse.success("操作成功");
    }

    @PostMapping(value = "/upStatusByChannel")
    @ApiOperation(value = "黑名单根据id停用合作商或者直营车商")
    public IResponse upStatusByChannel(@RequestBody List<Long> ids) {
        //截取字符串
        if (ids.size() > 0) {
            for (int j = 0; j < ids.size(); j++) {
                ChannelBaseInfo channel = channelBusiInfoService.getById(ids.get(j));
                if (channel != null) {
                    if (channel.getChannelStatus() != null) {
                        channel.setChannelStatus(Common.STATUS_STOP);
                    }
                    if (channel.getChannelStatusOldCar() != null) {
                        channel.setChannelStatusOldCar(Common.STATUS_STOP);
                    }
                    /*channel.setChannelStatus(Common.STATUS_STOP);
                    channel.setChannelStatusOldCar(Common.STATUS_STOP);*/
                    channel.setUpdateTime(new Date(System.currentTimeMillis()));
                    channel.setUpdateBy(SecurityUtils.getUsername());
                    boolean result = channelBusiInfoService.updateById(channel);

                    //渠道类型为直营车商的 同步停用中间关联表
                    if (Common.DIRECT_CAR_DEALER.equals(channel.getChannelType())) {
                        List<ChannelCoopeCardealer> cardealerTemps = channelCoopeCardealerService.list(Wrappers.<ChannelCoopeCardealer>query().lambda()
                                .eq(ChannelCoopeCardealer::getCardealerId, ids.get(j))
                                .eq(ChannelCoopeCardealer::getCardealerType, Common.DIRECT_CAR_DEALER)
                        );
                        if (cardealerTemps.size() > 0) {
                            for (ChannelCoopeCardealer temp : cardealerTemps) {
                                temp.setStatus(Common.STATUS_STOP);
                                channelCoopeCardealerService.updateById(temp);
                            }
                        }
                    }

                    if (!result) {
                        return IResponse.fail("状态更新失败！");
                    }
                }
            }
        }
        return new IResponse<Boolean>().setMsg("状态更新成功");
    }

    @PostMapping(value = "/upStatusByChannelPass")
    @ApiOperation(value = "黑名单根据id启用合作商或者直营车商")
    public IResponse upStatusByChannelPass(@RequestBody List<Long> ids) {
        if (ids.size() > 0) {
            for (int j = 0; j < ids.size(); j++) {
                ChannelBaseInfo channel = channelBusiInfoService.getById(ids.get(j));
                if (channel != null) {
                    channel.setChannelStatus(Common.STATUS_ENABLE);
                    channel.setChannelStatusOldCar(Common.STATUS_ENABLE);
                    channel.setUpdateTime(new Date(System.currentTimeMillis()));
                    channel.setUpdateBy(SecurityUtils.getUsername());
                    boolean result = channelBusiInfoService.updateById(channel);
                    if (!result) {
                        return IResponse.fail("状态更新失败！");
                    }
                }
            }
        }
        return new IResponse<Boolean>().setMsg("状态更新成功");
    }

    /**
     * 获取合作商/直营车商信息
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/channelInfoForUserName")
    @ApiOperation("获取合作商/直营车商信息")
    public IResponse<ChannelCaseInfoVo> getAffiliatedUnit(@RequestBody ChannelBaseInfoCondition condition) {

        ChannelBaseInfo temp = channelBusiInfoService.getOne(Wrappers.<ChannelBaseInfo>query().lambda()
                .eq(ChannelBaseInfo::getId, condition.getChannelId()));

        ChannelCaseInfoVo vo = new ChannelCaseInfoVo();
        if (temp != null) {
            //返回整个渠道信息
            vo.setChannelBaseInfo(temp);

            //查询风控信息
            List<ChannelRiskInfo> channelRiskInfoTempList = channelRiskInfoService.list(Wrappers.<ChannelRiskInfo>query().lambda().eq(ChannelRiskInfo::getChannelId, temp.getId().toString()));
            if (channelRiskInfoTempList != null && channelRiskInfoTempList.size() > 0) {
                //返回风控数据
                vo.setRisk(channelRiskInfoTempList);
            }
            //查询渠道保证金信息
            List<ChannelQuotaInfo> channelQuotaInfoTempList = channelQuotaInfoService.list(Wrappers.<ChannelQuotaInfo>query().lambda().eq(ChannelQuotaInfo::getChannelId, temp.getId().toString()));
            if (channelQuotaInfoTempList != null && channelQuotaInfoTempList.size() > 0) {
                //返回渠道保证金信息
                vo.setQuota(channelQuotaInfoTempList);
            }

            //查询渠道授权区域信息
            List<ChannelAuthorizeRegion> regionTempList = channelAuthorizeRegionService.list(Wrappers.<ChannelAuthorizeRegion>query().lambda().eq(ChannelAuthorizeRegion::getChannelId,
                    temp.getId().toString()));
            if (regionTempList != null && regionTempList.size() > 0) {
                //返回授权区域信息
                vo.setRegion(regionTempList);
            }

            List<ChannelReceivablesAccount> accountTempList = channelAccountInfoService.list(Wrappers.<ChannelReceivablesAccount>query().lambda().eq(ChannelReceivablesAccount::getChannelId,
                    temp.getId().toString()));
            if (accountTempList != null && accountTempList.size() > 0) {
                //返回收款账号信息
                vo.setAcount(accountTempList);
            }

            //查询授权车型
            List<ChannelAuthorizeVehicle> vehicleTempList = channelAuthorizeVehicleService.list(Wrappers.<ChannelAuthorizeVehicle>query().lambda().eq(ChannelAuthorizeVehicle::getChannelId,
                    temp.getId().toString()));
            if (vehicleTempList != null && vehicleTempList.size() > 0) {
                //返回授权车型
                vo.setVehicle(vehicleTempList);
            }
        }
        return IResponse.success(vo);
    }

    /**
     * 批量修改经纬度范围同步案件服务
     *
     * @param list
     * @return
     */
    @PostMapping(value = "/saveLlRangeToCase")
    @ApiOperation("批量修改经纬度范围同步案件服务")
    public IResponse<ChannelCaseInfoVo> saveLlRangeToCase(@RequestBody List<ChannelBaseInfo> list) {
        if (list != null && list.size() > 0) {
            channelBusiInfoService.saveOrUpdateBatch(list);
        }
        return IResponse.success("修改成功");
    }

    /**
     * 批量修改区域经理同步案件服务
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/saveManageToCase")
    @ApiOperation("批量修改经纬度范围同步案件服务")
    public IResponse saveManageToCase(@RequestBody ChannelBaseInfoCondition condition) {
        List<ChannelRiskInfo> riskInfoTempList = channelRiskInfoService.list(Wrappers.<ChannelRiskInfo>query().lambda().in(ChannelRiskInfo::getChannelId, Arrays.asList(condition.getChannelIds())));
        if (riskInfoTempList != null && riskInfoTempList.size() > 0) {
            riskInfoTempList.forEach(temp -> {
                if (BUSINESS_TYPE_NEW_CAR.equals(temp.getBusinessType())) {
                    if (!StrUtil.isBlank(condition.getCustomerManagerNew())) {
                        temp.setCustomerManager(condition.getCustomerManagerNew());
                    }
                }
                if (BUSINESS_TYPE_OLD_CAR.equals(temp.getBusinessType())) {
                    if (!StrUtil.isBlank(condition.getCustomerManagerOld())) {
                        temp.setCustomerManager(condition.getCustomerManagerOld());
                    }
                }
            });
            channelRiskInfoService.saveOrUpdateBatch(riskInfoTempList);
        }
        return IResponse.success("修改成功");
    }

    /**
     * 查询车辆类型
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/queryCarType")
    @ApiOperation("查询车辆类型")
    public IResponse queryCarType(@RequestBody ChannelBaseInfoCondition condition) {
        ChannelRiskInfo riskInfo = channelRiskInfoService.getOne(Wrappers.<ChannelRiskInfo>query().lambda()
                .eq(ChannelRiskInfo::getChannelId, condition.getChannelId())
                .eq(ChannelRiskInfo::getBusinessType, condition.getBusinessType()));
        if (riskInfo != null) {
            return IResponse.success(riskInfo.getCarType());
        } else {
            return IResponse.fail("查无数据");
        }
    }

    /**
     * 查询渠道账号
     *
     * @param channelId
     * @param businessType
     * @return
     */
    @PostMapping(value = "/queryAccountInfo")
    @ApiOperation("查询渠道账号")
    public IResponse queryAccountInfo(@RequestParam("channelId") Long channelId, @RequestParam("businessType") String businessType) {

        ChannelReceivablesAccount channelReceivablesAccount = channelAccountInfoService.getOne(Wrappers.<ChannelReceivablesAccount>query().lambda()
                .eq(ChannelReceivablesAccount::getChannelId, channelId)
                /**  业务类型 by ZC.GUO  **/
                .eq(ChannelReceivablesAccount::getAccountAttribute, businessType)
                /**  车款账号 by ZC.GUO  **/
                .eq(ChannelReceivablesAccount::getCollectionType, "1")
                .eq(ChannelReceivablesAccount::getStatus, "00")
        );
        if (ObjectUtil.isNotNull(channelReceivablesAccount)) {
            return IResponse.success(channelReceivablesAccount);
        } else {
            return IResponse.fail("获取账号信息失败");
        }
    }

    /**
     * 获取地区信息
     *
     * @param condition
     * @return
     */
    @Transactional
    @PostMapping(value = "/getAddressList")
    @ApiOperation(value = "获取数据")
    public IResponse<List<TsysAddressParam>> getAddressList(@RequestBody AddressParamCondition condition) {
        // 先查询所有合作商省的code
        List<String> pCode = new ArrayList<>();
        List<ChannelBaseInfo> baseInfos = channelBusiInfoService.list();
        if ("1".equals(condition.getLevel())) {
            // 说明需要查询所有省的
            if (baseInfos.size() > 0) {
                for (ChannelBaseInfo b : baseInfos) {
                    if (b.getChannelProvince() != null) {
                        pCode.add(b.getChannelProvince());
                    }
                }
            }
        } else {
            // 说明查询的是市
            if (baseInfos.size() > 0) {
                for (ChannelBaseInfo b : baseInfos) {
                    if (b.getChannelCity() != null) {
                        pCode.add(b.getChannelCity());
                    }
                }
            }
        }
        if (pCode.size() <= 0) {
            // 说明没有渠道合作商省
            throw new AfsBaseException("暂无合作商注册！");
        }
        List<TsysAddressParam> list = service.list(Wrappers.<TsysAddressParam>query().lambda()
                .eq(StringUtils.isNotEmpty(condition.getUpperCode()), TsysAddressParam::getUpperCode, condition.getUpperCode())
                .eq(StringUtils.isNotEmpty(condition.getLevel()), TsysAddressParam::getAddrLevel, condition.getLevel()).orderByAsc(TsysAddressParam::getValue).in(TsysAddressParam::getValue, pCode));
        //返回数据
        return new IResponse<List<TsysAddressParam>>().setData(list);
    }

    @RequestMapping(value = "/getCaseBaseInfo", method = RequestMethod.POST)
    @ApiOperation(value = "获取渠道基本信息")
    public IResponse getCaseBaseInfo(@RequestBody ChannelCaseInfoDTO dto) {
        List<ChannelBaseInfo> baseList = channelBusiInfoService.list(Wrappers.<ChannelBaseInfo>query().lambda()
                .like(!ObjectUtils.isEmpty(dto.getChannelFullName()),ChannelBaseInfo::getChannelFullName, dto.getChannelFullName()));
        List<ChannelCaseInfoDTO> dtoList = new ArrayList<>();
        if(baseList!=null && baseList.size()>0){
            for(ChannelBaseInfo temp:baseList){
                ChannelCaseInfoDTO infoDTO = new ChannelCaseInfoDTO();
                infoDTO.setChannelCode(temp.getChannelCode());
                infoDTO.setChannelFullName(temp.getChannelFullName());
                dtoList.add(infoDTO);
            }
        }
        return IResponse.success(dtoList);
    }

    @RequestMapping(value = "/getAllCaseBaseInfo", method = RequestMethod.POST)
    @ApiOperation(value = "获取渠道基本信息")
    public IResponse getAllCaseBaseInfo(@RequestBody CarDealerCondition condition) {

        IPage<ChannelBaseInfo> baseList = channelBusiInfoService.page(new Page(condition.getPageNumber(), condition.getPageSize()),Wrappers.<ChannelBaseInfo>query().lambda()
                .like(!ObjectUtils.isEmpty(condition.getSearchName()),ChannelBaseInfo::getChannelFullName, condition.getSearchName()));

        List<AllCarDealerVo> dealerVos=new ArrayList<AllCarDealerVo>();
        for(ChannelBaseInfo list:baseList.getRecords()){
            AllCarDealerVo vo=new AllCarDealerVo();
            vo.setLabel(list.getChannelCode());
            vo.setValue(list.getChannelFullName());
        }
        return IResponse.success(dealerVos);
    }
}
